﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcmyjobs.DAL
{
	/// <summary>
	/// 数据访问类:news
	/// </summary>
	public partial class news
	{
		public news()
		{}
		#region  Method
        public DataSet GetTJ(int atid,int count)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@atid", SqlDbType.Int,4),                   
                    new SqlParameter("@count", SqlDbType.Int,4)
};
            parameters[0].Value = atid;
            parameters[1].Value = count;

            DataSet ds = DbHelperSQL.RunProcedure("[dbo].[P_getTJnews]", parameters, "ds");
            //count = int.Parse(parameters[3].Value.ToString());
            return ds;
            
            //
            
        }
        public int deletenews(string uid)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@uid", SqlDbType.VarChar,200)
                   
};
            parameters[0].Value = uid;
            DbHelperSQL.RunProcedure("[dbo].[P_newdeletes]", parameters, out rowsAffected);
            return rowsAffected;

        }
        public DataSet GetListHotNews(int atd, int count)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@atid", SqlDbType.Int,4),                   
                    new SqlParameter("@count", SqlDbType.Int,4)
};
            parameters[0].Value = atd;
            parameters[1].Value = count;
           
            DataSet ds = DbHelperSQL.RunProcedure("[dbo].[P_GetListHotNews]", parameters, "ds");
            //count = int.Parse(parameters[3].Value.ToString());
            return ds;
        }     
        public DataSet GetListNews(int atd,int count,int typid)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@atid", SqlDbType.Int,4),
                    new SqlParameter("@typeid", SqlDbType.Int,4),
                    new SqlParameter("@count", SqlDbType.Int,4)
};
       parameters[0].Value = atd;
       parameters[1].Value = typid;
       parameters[2].Value = count;
            DataSet ds = DbHelperSQL.RunProcedure("[dbo].[P_GetListNews]", parameters, "ds");
            //count = int.Parse(parameters[3].Value.ToString());
            return ds;
        }        
        public DataSet GetNews(int nid)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@nid", SqlDbType.Int,4)
};
            
            parameters[0].Value = nid;

            DataSet ds = DbHelperSQL.RunProcedure("[dbo].[P_getnews]", parameters, "ds");
            //count = int.Parse(parameters[3].Value.ToString());
            return ds;
        }
        public DataSet PageNews(int atid, int startid, int pagesize,int typeid, out int count)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@atid", SqlDbType.Int,4),
                    new SqlParameter("@startIndex", SqlDbType.Int,4),
                    new SqlParameter("@endIndex", SqlDbType.Int,4),
                    new SqlParameter("@typeid", SqlDbType.Int,4),
                    new SqlParameter("@count", SqlDbType.Int,4)
};
            parameters[4].Direction = ParameterDirection.Output;
            parameters[0].Value = atid;
            parameters[1].Value = startid;
            parameters[2].Value = startid + pagesize - 1;
            parameters[3].Value = typeid;
            DataSet ds = DbHelperSQL.RunProcedure("[dbo].[P_PageNewsMain]", parameters, "ds");
            count = int.Parse(parameters[4].Value.ToString());
            return ds;
        }
        public DataSet PageNews(int atid, int startid, int pagesize, out int count)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@atid", SqlDbType.Int,4),
                    new SqlParameter("@startIndex", SqlDbType.Int,4),
                    new SqlParameter("@endIndex", SqlDbType.Int,4),
                    new SqlParameter("@count", SqlDbType.Int,4)
};
            parameters[3].Direction = ParameterDirection.Output;
            parameters[0].Value = atid;
            parameters[1].Value = startid;
            parameters[2].Value = startid + pagesize - 1;
            DataSet ds = DbHelperSQL.RunProcedure("[dbo].[P_PageNews]", parameters, "ds");
            count = int.Parse(parameters[3].Value.ToString());
            return ds;
        }

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("nid", "news"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int nid)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@nid", SqlDbType.Int,4)};
			parameters[0].Value = nid;

			int result= DbHelperSQL.RunProcedure("news_Exists",parameters,out rowsAffected);
			if(result==1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		///  增加一条数据
		/// </summary>
		public bool Add(SqlParameter[] parameters)
		{
			int rowsAffected;		

			DbHelperSQL.RunProcedure("news_ADD",parameters,out rowsAffected);

            if (rowsAffected > 0)
                return true;
            else
                return false;
           
        }

        /// <summary>
        /// 批量个更改
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="value">更改值</param>
        /// <param name="where">更改条件enterprise_rid</param>
        /// <returns></returns>
        public bool UpdateWhere(string keywords, string value, string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update news set " + keywords + " = '" + value + "' ");
            strSql.Append(" where nid in (" + where + ")  ");

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量个更改
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="value">更改值</param>
        /// <param name="where">更改条件enterprise_rid</param>
        /// <returns></returns>
        public bool UpdateWherestr(string keywords, string value, string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update news set " + keywords + " = @value ");

            if (!string.IsNullOrEmpty(where))
            {
                strSql.Append(" where "+where);
            }

            SqlParameter[] para = { new SqlParameter("@value",SqlDbType.Text)};

            para[0].Value = value;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(),para);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		///  更新一条数据
		/// </summary>
		public bool Update(lcmyjobs.Model.news model)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@nid", SqlDbType.Int,4),
					new SqlParameter("@ntitle", SqlDbType.NVarChar,50),
					new SqlParameter("@ntime", SqlDbType.DateTime),
					new SqlParameter("@nauthor", SqlDbType.NVarChar,20),
					new SqlParameter("@viewtime", SqlDbType.Int,4),
					new SqlParameter("@ncontent", SqlDbType.Text),
					new SqlParameter("@ntid", SqlDbType.Int,4)};
			parameters[0].Value = model.nid;
			parameters[1].Value = model.ntitle;
			parameters[2].Value = model.ntime;
			parameters[3].Value = model.nauthor;
			parameters[4].Value = model.viewtime;
			parameters[5].Value = model.ncontent;
			parameters[6].Value = model.ntid;

			DbHelperSQL.RunProcedure("news_Update",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int nid)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@nid", SqlDbType.Int,4)};
			parameters[0].Value = nid;

			DbHelperSQL.RunProcedure("news_Delete",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string nidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from news ");
			strSql.Append(" where nid in ("+nidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcmyjobs.Model.news GetModel(int nid)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@nid", SqlDbType.Int,4)};
			parameters[0].Value = nid;

			lcmyjobs.Model.news model=new lcmyjobs.Model.news();
			DataSet ds= DbHelperSQL.RunProcedure("news_GetModel",parameters,"ds");
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["nid"]!=null && ds.Tables[0].Rows[0]["nid"].ToString()!="")
				{
					model.nid=int.Parse(ds.Tables[0].Rows[0]["nid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ntitle"]!=null && ds.Tables[0].Rows[0]["ntitle"].ToString()!="")
				{
					model.ntitle=ds.Tables[0].Rows[0]["ntitle"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ntime"]!=null && ds.Tables[0].Rows[0]["ntime"].ToString()!="")
				{
					model.ntime=DateTime.Parse(ds.Tables[0].Rows[0]["ntime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["nauthor"]!=null && ds.Tables[0].Rows[0]["nauthor"].ToString()!="")
				{
					model.nauthor=ds.Tables[0].Rows[0]["nauthor"].ToString();
				}
				if(ds.Tables[0].Rows[0]["viewtime"]!=null && ds.Tables[0].Rows[0]["viewtime"].ToString()!="")
				{
					model.viewtime=int.Parse(ds.Tables[0].Rows[0]["viewtime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ncontent"]!=null && ds.Tables[0].Rows[0]["ncontent"].ToString()!="")
				{
					model.ncontent=ds.Tables[0].Rows[0]["ncontent"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ntid"]!=null && ds.Tables[0].Rows[0]["ntid"].ToString()!="")
				{
					model.ntid=int.Parse(ds.Tables[0].Rows[0]["ntid"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select *,isrmname=(case isrm when 0 then '否' else  '是' end),istjname=(case istj when 0 then '否' else  '是' end) ");
			strSql.Append(" FROM Vnews ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" nid,ntitle,ntime,nauthor,viewtime,ncontent,ntid ");
			strSql.Append(" FROM news ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "news";
			parameters[1].Value = "nid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

